# load library
# remove(list=ls())
# setwd(dirname(rstudioapi::getActiveDocumentContext()$path)); 
library("dplyr")

# read data
main = as.data.table(fread("main_cleaned.csv"))
main[,TFP_full:=as.numeric(TFP_full)]

# generate plots for Altman Z score --------------------------------------------------------
main %>% 
  ggplot(aes(Altman_Z_full, TFP_full)) +
  geom_point(alpha=0.05)+ geom_bin2d(bins = 500)+ 
  geom_smooth(method=lm) +
  scale_x_continuous(name = "Altman-Z", limits = c(-5,40)) + scale_y_continuous(limits=c(0,4), name="TFP") +
  theme_bw() +   theme(text = element_text(family = "serif"))
ggsave("../Figures/FigureA5a_altman_1.jpeg", width = 5, height=4)


main %>% 
  # filter(fyear==2019) %>%
  mutate(bin = ntile(Altman_Z, n=25)) %>%
  group_by(bin) %>%
  summarise(TFP_mean = mean(TFP), 
            TFP_sd = sd(TFP), 
            Altman_Z_mean = mean(Altman_Z),
            Altman_Z_sd = sd(Altman_Z)) %>%
  mutate(TFP_se = TFP_sd/sqrt(n())) %>%
  ggplot(aes(x=Altman_Z_mean, y=TFP_mean)) + 
  geom_point()+
  geom_errorbar(aes(ymax=TFP_mean+2*TFP_se, ymin=TFP_mean-2*TFP_se))+
  scale_x_continuous(name = "Altman-Z") + ylab("TFP") + 
  theme_bw() +   theme(text = element_text(family = "serif"))
ggsave("../Figures/FigureA5b_altman_2.jpeg", width=5,height=4)





